Backtracking in Git


Showing Latest Commit Logs

In Git, the commit you are currently on is known as the HEAD commit. The git show HEAD command will output everything the git log command display for the HEAD commit and all the file changes that were made when committed.

Reset Using SHA

Let's say you want to go back to a previous commit, how does one do that? Well you use the
git reset commit_SHA command, and what it does is set the HEAD commit to the commit_SHA commit. To use git reset, you will need to use the first seven digits of the previous commit's SHA, and that can be found using the git log command to see records of previous commits.

Remove File from Staging

Let's say you've added a file's changes to the staging area, but you change your mind, you can do the following, git reset HEAD <filename>, and what it does is remove the file from the staging area.

Note: This does not discard changes made in the working directory

You can use git status to make sure your file was properly removed from the staging area.

Going Back to the Last Commit

Let's say you want to go back to the changes made in a file in your last commit, or the HEAD commit. To do this, you can use git checkout HEAD <filename> and it will make the file in the working directory to go back to the changes made in the last commit.

You can make sure it worked by using git diff command to see if the rollback was successful. If nothing is outputted, that means it worked.